﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LingDong.IndexEntities.Document;
using LingDong.HtmlParser;

namespace LingDong.LinkAnalyser
{
    internal class PageQuality
    {
        internal static double[] GetPageQualityRank(IndexReader.Index index)
        {
            int documentCount = index.GetTotalDocumentCount();
            double[] rank = new double[documentCount];
            for (int i = 0; i < documentCount; i++)
            {
                OriginalDocumentEnity doc = index.GetOriginalDocument(i);
                rank[i] = GetRank(doc.URL, doc.Content);
            }

            // normalization
            double sum = rank.Sum();
            for (int i = 0; i < documentCount; i++)
            {
                rank[i] /= sum;
            }

            return rank;
        }

        private static double GetRank(string url, string html)
        {
            double rank = 0.0;
            string title = String.Empty;
            string content = HtmlContent.ParseHtml(html, out title);

            // content length, the more the better, 500 at most
            rank += Math.Max(content.Length, 500) / 10.0;

            // title length, the more the better, 50 at most
            // page that title length > 200 is treated as spam
            int len = title.Length;
            if (len < 200)
                rank += Math.Max(title.Length, 50);

            // url depth, the less the better
            int depth = HtmlUtility.GetFullDir(url).Count;
            rank += 50.0 / depth;

            return rank;
        }
    }
}
